package com.fivefu.core.data.controller;

import com.alibaba.fastjson2.JSON;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.controller.BaseController;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.core.data.entity.DbFfskyeyeRegion;
import com.fivefu.core.data.services.DbFfskyeyeRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 域 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-09-01 11:12:00
 */
@Api(tags = "区域组织架构汇总")
@RestController
@RequestMapping("/region")
public class DbFfskyeyeRegionController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(DbFfskyeyeRegionController.class);

    @Autowired
    private DbFfskyeyeRegionService dbFfskyeyeRegionService;

    @ApiOperation(value = "区域信息汇总入库",httpMethod = "GET",response = ResultInfo.class,notes = "区域信息汇总入库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "设备平台：海康，大华",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId",value = "用户编号",paramType = "query",required = true, dataType = "String")
    })
    @RequestMapping(path = "/regionCollect",method = RequestMethod.GET)
    public ResultInfo regionCollect(String type,String userId) {
        try {
            if (StrUtils.isNull(type)) {
                return ResultInfo.renderError("设备平台不能为空");
            }
            if (StrUtils.isNull(userId)) {
                return ResultInfo.renderError("用户编号不能位空");
            }
            dbFfskyeyeRegionService.regionCollect(type,userId);
            return ResultInfo.renderSuccess("区域信息汇总入库成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return  ResultInfo.renderError("区域信息汇总入库失败");
    }

    @ApiOperation(value = "编辑文件夹",httpMethod = "POST",response = ResultInfo.class,notes = "编辑文件夹")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "regionName",value = "文件分类名",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "pid",value = "父级文件组织编号",paramType = "query",required = false, dataType = "Long"),
            @ApiImplicitParam(name = "platform",value = "平台标识",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "id",value = "文件组织id",paramType = "query",required = false, dataType = "Long"),
    })
    @RequestMapping(path = "/editRegion",method = RequestMethod.POST)
    public ResultInfo editRegion(String regionName,Long pid,Long id, String platform, @RequestAttribute String userid) {
        try {
            logger.info("编辑文件夹接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (StrUtils.isNull(regionName)) {
                return ResultInfo.renderError("分类名称不能为空");
            }
            if (StrUtils.isNull(platform)) {
                return ResultInfo.renderError("平台标识不能为空");
            }
            if (null == pid) {
                pid = 0L;
            }
            DbFfskyeyeRegion pRegion = dbFfskyeyeRegionService.queryRegionByRegionName(regionName, platform, pid, null);
            if (null != pRegion) {
                if (id != null && pRegion.getId().equals(id)){
                    return ResultInfo.renderError("文件夹名称未修改");
                }
                return ResultInfo.renderError("编辑文件夹失败");
            }

            DbFfskyeyeRegion region = new DbFfskyeyeRegion();
            if (id != null) {
                region.setId(id);
            }
            region.setName(regionName);
            region.setPlatformCode(platform);
            if (pid != null) {
                region.setParentid(pid);
            }
            region.setType("CT08");//虚拟区域

            Long regionId = dbFfskyeyeRegionService.editRegionById(region, userid);
            return ResultInfo.renderSuccess(200,"编辑文件夹成功",regionId);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("编辑文件夹失败");
    }
}
